package de.listrong.da.main;

import gate.*;
import gate.creole.ResourceInstantiationException;
import gate.creole.Transducer;
import gate.creole.gazetteer.OntoGazetteer;
import gate.creole.ontology.Ontology;
import gate.creole.splitter.SentenceSplitter;
import gate.creole.tokeniser.DefaultTokeniser;
import gate.gui.MainFrame;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.log4j.Logger;

public class GateProcess {
    public static Logger myLogger   = Logger.getLogger(GateProcess.class);

    static private String GATE_HOME = "E:\\gate";
    static private String URER_CONF = "E:\\workspace34\\obit\\settings\\gate.xml";
    static private String PLUGIN_HOME = "E:\\workspace34\\obit\\plugins\\";

    /**
     * initial method for GATE Framework
     * @param: boolean displayGUI, set the display for GATE-GUI
     */
    public static void init(boolean displayGUI) {
        try {
            // GATE Configuration
            Gate.setGateHome(new File(GATE_HOME));

            // load the gate configuration from the gate.XML. it is important
            // for the GATE with other Application.
            Gate.setUserConfigFile(new File(URER_CONF));
            Gate.setPluginsHome(new File(PLUGIN_HOME));

            /* GATE Initialization */
            Gate.init();

            /* tell GATE about the ANNIE plugin */
            File ontoHome = new File(Gate.getPluginsHome(), "Ontology_Tools");
            Gate.getCreoleRegister().addDirectory(ontoHome.toURI().toURL());

            if (displayGUI) {
                showGUI();
            }
        } catch (Exception e) {
            e.printStackTrace();
            myLogger.error("Error by GateProcess.init()" + e);
        } // end try ... catch
        
        myLogger.info("GateProcess init successful");
    }

    /**
     * @param fileList
     * @return Corpus
     * @throws ResourceInstantiationException
     * @throws MalformedURLException
     */
    @SuppressWarnings("unchecked")
    public static Corpus loadDocIntoCorpus(List<String> fileList)
            throws ResourceInstantiationException, MalformedURLException {
        Corpus docCorpus = Factory.newCorpus("corpus with " + fileList.size()
                + " files");
        int id = 0;
        for (String urlStr : fileList) {
            Document doc = Factory.newDocument(new URL(urlStr));
            doc.getFeatures().put("id", id);
            docCorpus.add(doc);
        }
        return docCorpus;
    }

    /**
     * @param ontoURL
     * @param defaultNamespace
     * @return Ontology
     * @throws MalformedURLException
     * @throws ResourceInstantiationException
     */
    public static Ontology loadOntology(URL ontoURL, String defaultNamespace)
            throws MalformedURLException, ResourceInstantiationException {
        // set the parameters
        FeatureMap fm = Factory.newFeatureMap();
        fm.put("rdfXmlURL", ontoURL);
        fm.put("defaultNameSpace", defaultNamespace);
        // create an instance of ontology
        Ontology ontology = (Ontology) Factory.createResource(
                "gate.creole.ontology.owlim.OWLIMOntologyLR", fm, null,
                "MedOnto");
        return ontology;
    }

    /**
     * this method create a gate default tokeniser object
     *
     * @return gate.creole.tokeniser.DefaultTokeniser
     * @throws MalformedURLException
     * @throws ResourceInstantiationException
     */
    public static DefaultTokeniser loadTokeniser()
            throws ResourceInstantiationException {
        DefaultTokeniser tokeniser;

        FeatureMap featureValues = Factory.newFeatureMap();
        FeatureMap parameterValues = Factory.newFeatureMap();
        URL rulesURL;
        URL grammURL;
        try {
            rulesURL = new URL(
                    "file:/E:/workspace34/obit/plugins/ANNIE/resources/tokeniser/DefaultTokeniser.rules");
            grammURL = new URL(
                    "file:/E:/workspace34/obit/plugins/ANNIE/resources/tokeniser/postprocess.jape");

            parameterValues.put("tokeniserRulesURL", rulesURL);
            parameterValues.put("transducerGrammarURL", grammURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        tokeniser = (DefaultTokeniser) Factory.createResource(
                "gate.creole.tokeniser.DefaultTokeniser", parameterValues,
                featureValues, "default Englisch Tokeniser");

        return tokeniser;
    }

    /**
     * this method create a default gate sentence splitter
     *
     * @return gate.creole.splitter.SentenceSplitter
     * @throws MalformedURLException, ResourceInstantiationException
     */
    public static SentenceSplitter loadSentenceSplitter()
            throws MalformedURLException, ResourceInstantiationException {
        FeatureMap featureValues = Factory.newFeatureMap();
        FeatureMap parameterValues = Factory.newFeatureMap();
        URL gazetteerListsURL = new URL(
                "file:/E:/workspace34/obit/plugins/ANNIE/resources/sentenceSplitter/gazetteer/lists.def");
        URL transducerURL = new URL(
                "file:/E:/workspace34/obit/plugins/ANNIE/resources/sentenceSplitter/grammar/main.jape");
        parameterValues.put("gazetteerListsURL", gazetteerListsURL);
        parameterValues.put("transducerURL", transducerURL);
        parameterValues.put("outputASName", "Sentences");
        SentenceSplitter splitter = (SentenceSplitter) Factory.createResource(
                "gate.creole.splitter.SentenceSplitter", parameterValues,
                featureValues, "SentenceSplitter");
        return splitter;
    }

    /* OntoGazetteer */
    public static OntoGazetteer loadOntoGazetteer()
            throws MalformedURLException, ResourceInstantiationException {
        OntoGazetteer ontoGazetteer;
        FeatureMap gazetteerFeatures = Factory.newFeatureMap();
        FeatureMap gazetteerParams = Factory.newFeatureMap();
        URL mappingURL = new URL(
                "file:/e:/workspace34/obit/plugins/TheFinder/mapping.def");
        URL listsURL = new URL(
                "file:/e:/workspace34/obit/plugins/TheFinder/lists.def");
        gazetteerParams.put("mappingURL", mappingURL);
        gazetteerParams.put("listsURL", listsURL);
        gazetteerParams.put("gazetteerName",
                "com.ontotext.gate.gazetteer.HashGazetteer");
        ontoGazetteer = (OntoGazetteer) Factory.createResource(
                "gate.creole.gazetteer.OntoGazetteerImpl", gazetteerParams,
                gazetteerFeatures, "OntoGazetteer");
        return ontoGazetteer;
    }

    /* JAPE Transducer for drug instances */
    public static Transducer loadJapeTransducer(Ontology ontology)
            throws MalformedURLException, ResourceInstantiationException {
        FeatureMap params = Factory.newFeatureMap();
        params.put("grammarURL", new URL(
                "file:/e:/workspace34/obit/plugins/TheFinder/drug.jape"));
        params.put("outputASName", "Instances");
        params.put("ontology", ontology);
        Transducer japeTransducer = (Transducer) Factory.createResource(
                "gate.creole.Transducer", params, null, "JAPE Transducer");
        return japeTransducer;
    }

    /* JAPE Transducer for drugcombination */
    public static Transducer loadJapeTransducerNoOnto()
            throws MalformedURLException, ResourceInstantiationException {
        FeatureMap params = Factory.newFeatureMap();
        params.put("grammarURL", new URL(
                "file:/e:/workspace34/obit/plugins/TheFinder/drugcombination.jape"));
        params.put("outputASName", "Instances");
        Transducer japeTransducer = (Transducer) Factory.createResource(
                "gate.creole.Transducer", params, null, "JAPE Transducer");
        return japeTransducer;
    }

    /* show GATE GUI */
    private static void showGUI() {
        MainFrame mainFrame = new MainFrame();
        System.out.print(mainFrame.getTitle());
        mainFrame.setSize(1024, 768);
        mainFrame.setVisible(true);
    }

}
